/**
 * Created by yunpeng on 2015/4/16.
 */
var Table = require('../utils/Table').Table;
var data = require('./carbrands');
var request = require('request');
var async = require('async');
var series_data = data.brand.items;

var number=0;
async.whilst(function(){
    return number<series_data.length;
},function(callback) {
    request.get({url:'http://www.souche.com/pages/dicAction/loadRootLevelForCar.json?type=car-subdivision&code='+series_data[number].code},function(error, httpResponse, body){
        var docs=JSON.parse(body);
        var carbrand=[];
        var carnum=[];
        var res={};
        async.waterfall([
            function(callback){
                var count=0;
                async.whilst(function(){
                    return count<docs.keys.length;
                },function(callback) {
                    var brand=docs.keys[count];
                    var code={};
                    code.car=docs.codes[brand];
                    //console.log(code.car);
                    var cars=[];
                    for(var i=0;i<code.car.length;i++){
                        cars.push(code.car[i].name);

                    }
                    carbrand.push(brand);
                    carnum.push(cars);
                    count++;
                    callback();
                },function(err){

                });
                res.carbrand=carbrand;
                res.carnum=carnum;
                callback(null, res);
            }
        ], function (err, res) {
            var coun=0;
            async.whilst(function(){
                return coun<res.carbrand.length;
            },function(callback) {
                new Table().getConnection(function (connection) {
                    connection.beginTransaction(function (err) {
                        if (err) { //throw err;
                            callback(false);
                        }
                        console.log(coun);
                        connection.query('INSERT INTO carbrand SET ?', {carbrand: res.carbrand[coun]}, function (err, result) {
                            if (err) {
                                connection.rollback(function () {
                                    //throw err;
                                    callback(false);
                                });
                            }
                            var len=res.carnum[coun].length;
                                var aaa=0;
                                async.whilst(function(){
                                    return aaa<len;
                                },function(callback) {
                                    var carnum = {cbid: result.insertId, num: res.carnum[coun][aaa], createtime: new Date()};

                                    connection.query('INSERT INTO carnum SET ?', carnum, function (err, result) {
                                        if (err) {
                                            connection.rollback(function () {
                                                //throw err;
                                                callback(false);
                                            });
                                        }
                                    });
                                    aaa++;
                                    callback();
                                },function(err){

                                });
                            coun++;

                            connection.commit(function(err) {
                                if (err) {
                                    connection.rollback(function() {
                                        //throw err;
                                        /*callback(false);*/
                                    });
                                }else{
                                    connection.release(); //release
                                }
                            });
                            callback();
                        });
                    });
                });
            },function(err){

            });
        });
    });
    number++;
    callback();
},function(err){

});

